package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hngm.bean.Newssheet;
import com.hngm.bean.Newstype;
import com.hngm.bean.Producttypetable;
import com.hngm.common.Result;
import com.hngm.common.ResultList;
import com.hngm.dao.NewssheetDao;
import com.hngm.dao.NewstypeDao;
import com.sun.media.sound.RealTimeSequencerProvider;

/**
 * Servlet implementation class Article_listServlet
 */
public class Article_listServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Article_listServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//获取请求中的参数
		String url = request.getParameter("url");
		//创建dao
		NewssheetDao newssheetDao = new NewssheetDao();
		//创建NewstypeDao
		NewstypeDao newstypeDao = new NewstypeDao();
		if("newList".equals(url)){
			//查询新闻列表和新闻类型
			//获前端页面传过来的新闻类型
			String nType = request.getParameter("nType");
			
			//根据上面获取到的新闻类型去查询对应的新闻信息
			List<Newssheet> ntList = newssheetDao.queryNewssheetByType(nType);
			request.setAttribute("ntList", ntList);
			//查询新闻类型
			ArrayList<Newstype> atList = newstypeDao.queryNewstypeList();
			request.setAttribute("atList", atList);
			request.getRequestDispatcher("article_list.jsp").forward(request, response);
		}else if("newContent".equals(url)){
			//根据新闻ID查询新闻内容
			String pid = request.getParameter("pid");
			//查询数据库
			//查询新闻内容
			Newssheet n = newssheetDao.queryNewssheetInfoById(pid);
			request.setAttribute("newssheetInfo", n);
			
            String nType = n.getNewstype_id() + "";
			
			//根据上面获取到的新闻类型去查询对应的新闻信息
			List<Newssheet> ntList = newssheetDao.queryNewssheetByType(nType);
			request.setAttribute("ntList", ntList);
			//查询新闻类型
			ArrayList<Newstype> atList = newstypeDao.queryNewstypeList();
			request.setAttribute("atList", atList);
			
			//去记录当前用户点击的是哪一种类型
			for(int i = 0 ; i < atList.size(); i++){
				if(nType.equals(atList.get(i).getId()+"")){
					request.setAttribute("cType", atList.get(i));
				}
			}
			//转发到新闻内容页面
			request.getRequestDispatcher("article_list_content.jsp").forward(request, response);
		}else if("querynewsListByParam".equals(url)){
			//获取前端传过来的查询条件
			String newsname = request.getParameter("newsname");
			String del = request.getParameter("del");
			String orderby = request.getParameter("orderby");
			//获取LayUI穿过来的分页信息
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = Integer.parseInt(request.getParameter("page"));
			
			//当前页开始的索引
			int beginIndex = (page - 1) * limit;
			
			//根据传过来得查询条件去查询新闻列表
			ArrayList<Newssheet> pList = newssheetDao.querynewsListByParam(newsname,del,orderby,beginIndex,limit);
			
			//查询数据库中的数量
			int count = newssheetDao.querynewsListByParamCount(newsname,del,orderby);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", count, pList);
			out.print(str);
		}else if("add".equals(url)){
			//添加新闻类型逻辑
			//1、获取前端传过来的信息
			String name = request.getParameter("name");
			Newstype p = new Newstype();
			p.setName(name);
			//2、新闻类型信息插入到表中（调用DAO）
			int result = NewstypeDao.insertNewstype(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		}else if("del".equals(url)){
			//删除新闻类型逻辑
			//1、获取前端传过来的产品Id
			String newstypeId = request.getParameter("newstypeId");
			//2、根据产品Id删除新闻信息
			int result = newstypeDao.delnewstype(newstypeId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		
		}else if("queryNewstypeList".equals(url)){
			//获取前端传过来的查询条件
			String name = request.getParameter("name");
			String del = request.getParameter("del");	
			//根据传过来得查询条件去查询新闻列表
			ArrayList<Newstype> pList = newstypeDao.querynewstypeByParam(name,del);
			
			//查询数据库中的数量
			int count = newstypeDao.querynewstypeByParamCount(name,del);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", count, pList);
			out.print(str);
		}else if("newstypeList".equals(url)){
			//查询新闻类型
			ArrayList<Newstype> naList = newstypeDao.queryNewstypeList();
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品类型列表转换成JSON格式的字符串
			String str = ResultList.toClient(0, "成功", 0, naList);
			out.print(str);
		}else if("edit".equals(url)){
			//编辑新闻类型逻辑
			//1、获取前端传过来的信息
			String name = request.getParameter("name");
			long id = Long.parseLong(request.getParameter("id"));
			Newstype p = new Newstype();
			p.setName(name);
			p.setId(id);
			//2、产品类型信息插入到表中（调用DAO）
			int result = NewstypeDao.editNewstype(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "编辑成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "编辑失败"));
			}
		
		}
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
